< previous page page_373 next page >

Page 373
Public Property Let Department(ByVal NewDept As Departments)
nDeptNum = NewDept
End Property
You could then access the Department property from code as follows:
objEmployee.Department = InformationTech
If objEmployee.Department = InformationTech Then MsgBox "IT department"
You can test this out by adding the Enum to the sample project you created earlier. Notice that as you type the equals sign to assign or read the value of the Department property, a list of valid values is displaed in a drop-down list.
Creating Classes That Contain Collections
A collection is a group of items that is itself a type of object. Visual Basic has some built-in collection objects, such as the Forms collection and the Controls collection. You can use collections with the For Each . . . Next statement to perform actions on all the items they contain. The MinimizeAll procedure, shown in Listing 16.1, minimizes each loaded form in an application.
Listing 16.1 COLLECT.ZIPUsing the For Each Structure to Handle All the Items in a Collection
' Minimizes all loaded forms.
Sub MinimizeAll()
    Dim frmElement As Form
    ' For each loaded form.
    For Each frmElement In Forms
        ' Minimize the form.
        frmElement.WindowState = vbMinimized
    Next frmElement
End Sub
Collections solve three problems faced by most programmers when working with objects:
They provide a standardized way to create and track multiple instances of an object.
They group similar objects for fixed tasks, such as changing color properties or dragging to a new location.
Unlike an array, a collection can be used to contain members of different types.
They organize large systems of objects into a hierarchy.
The following sections describe each of these aspects of using collections when you are creating object-based applications in Visual Basic.
Standard Collection Properties and Methods
User-defined collections share a common set of properties and methods. Some collections may have additional properties and methods, but all collections that you create in Visual Basic have at least the set described in Table 16.2.

 
< previous page page_373 next page >